package com.study.zzedu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.DiscussItem;
import com.study.zzedu.entity.NoticeItem;

import java.util.List;

/**
 * Created by kiros on 2020/9/6.
 * 在线讨论adapter
 */
public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.ViewHolder> {

    private Context context;
    private List<NoticeItem> noticeItems;

    public NoticeAdapter(Context context, List<NoticeItem> noticeItems) {
        this.context = context;
        this.noticeItems = noticeItems;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.notice_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        NoticeItem nItem = noticeItems.get(position);
//        if (position == 0) {
//            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.ll_notice.getLayoutParams();
//            layoutParams.setMargins(0, 20, 0, 0);
//            holder.ll_notice.setLayoutParams(layoutParams);
//        }
        holder.ll_notice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listner != null){
                    listner.onClick(position);
                }
            }
        });
        holder.tvTitle.setText(nItem.getTitle());
        holder.tvTime.setText(nItem.getTime());
        if(String.valueOf(nItem.getStatus()).equals("1")){
            holder.tv_red_point.setVisibility(View.GONE);
        }else {
            holder.tv_red_point.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return noticeItems.size();
    }

    public interface OnItemClickListner{
        void onClick(int position);
    }
    private OnItemClickListner listner;

    public void setOnItemClickListener(OnItemClickListner listener){
        this.listner = listener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_notice;
        private TextView tvTitle,tvTime,tv_red_point;
        public ViewHolder(View itemView) {
            super(itemView);
            ll_notice = itemView.findViewById(R.id.ll_discuss_item);
            tv_red_point = itemView.findViewById(R.id.tv_red_point);
            tvTitle = itemView.findViewById(R.id.tv_notice_title);
            tvTime = itemView.findViewById(R.id.tv_notice_time);
        }
    }
}